package com.wn.class30.cotroller.houtai;

import com.wn.class30.entity.AdminUser;
import com.wn.class30.entity.Comment;
import com.wn.class30.entity.Product;
import com.wn.class30.mapper.UserDao;
import com.wn.class30.service.CommentService;
import com.wn.class30.service.ProductService;
import com.wn.class30.tools.Page;
import com.wn.class30.tools.Result;
import com.wn.class30.tools.StringToArray;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/houtai/page/products/comment/")
public class CommentController {

    @Resource
    private CommentService commentService;
    @Resource
    private UserDao userDao;
    @Resource
    private ProductService productService;


    @RequiresPermissions("/houtai/page/products/comment/list")
    @RequestMapping("list")
    public Result selectComments(Integer page,Integer limit,String isanswer,String startPoint,String endPoint,String startDate,String endDate)throws Exception{
        log.info("page=============="+page);
        Subject subject = SecurityUtils.getSubject();
        AdminUser adminUser = (AdminUser) subject.getPrincipal();
        Map<String,String> map = new HashMap<>();
        map.put("page",String.valueOf((page-1)*limit));
        map.put("limit",String.valueOf(limit));
        map.put("isanswer",isanswer);
        map.put("startPoint",startPoint);
        map.put("endPoint",endPoint);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        if(adminUser.getBusinessId() != 0){
            map.put("businessId",adminUser.getBusinessId()+"");
        }
        Long orderCount = commentService.selectCommponentCount(map);
        Page p = new Page();
        p.setCurrentPage((page-1)*limit);
        p.setTotalSize(orderCount);
        p.setPageSize(limit);
        long pageSize = orderCount % limit == 0? orderCount / limit:orderCount / limit + 1;
        p.setPageSize((int)pageSize);
        List<Comment> comments = commentService.selectCommponents(map);
        for (Comment comment : comments) {
            comment.setUser(userDao.selectByPrimaryKey(comment.getUserId()));
            comment.setProduct(productService.selectProductById(comment.getCommentId()));
            comment.setCommentImg(commentService.selectCommentImgsById(comment.getCommentId()));
        }
        return new Result("0","success",p,comments);


    }

    @RequestMapping("update")
    @RequiresPermissions("/houtai/page/products/comment/update")
    public Result commentAnswer(@NotNull Integer commentId,@NotNull String commentAnswer)throws Exception{
        if(commentId != null && commentAnswer != null && !"".equals(commentAnswer.trim())){
            Comment comment = commentService.selectCommentById(commentId);
            comment.setCommentAnswer(commentAnswer);
            comment.setIsanswer(1);
            commentService.updateCommentAnswerById(comment);
            return new Result("200","success",null,null);
        }
        return new Result("500","error",null,null);
    }

    @RequiresPermissions("/houtai/page/products/comment/delete")
    @RequestMapping("delete")
    public Result deleteCommentById(@NotNull String commentIds)throws Exception{
        if(commentIds != null && !"".equals(commentIds.trim())){
            Integer[] array = StringToArray.getArray(commentIds);
            for (Integer integer : array) {
                commentService.deleteCommentById(integer);
                commentService.deleteCommentImgByCommentId(integer);
            }
            return new Result("200","success",null,null);
        }
        return new Result("500","error",null,null);
    }
}
